package com.ruoyi.basedata.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.basedata.domain.UsersXb;
import com.ruoyi.basedata.mapper.UsersXbMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.UserAccountMapper;
import com.ruoyi.basedata.domain.UserAccount;
import com.ruoyi.basedata.service.IUserAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 当前缴费信息Service业务层处理
 * 
 * @author gdqt
 * @date 2025-06-27
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements IUserAccountService
{
    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UsersXbMapper usersXbMapper;

    /**
     * 查询当前缴费信息
     * 
     * @param id 当前缴费信息主键
     * @return 当前缴费信息
     */
    @Override
    public UserAccount selectUserAccountById(Long id)
    {

        return userAccountMapper.selectById(id);
    }

    /**
     * 查询当前缴费信息列表
     * 
     * @param userAccount 当前缴费信息
     * @return 当前缴费信息
     */
    @Override
    public List<UserAccount> selectUserAccountList(UserAccount userAccount)
    {
        LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(userAccount.getUserId())){
            queryWrapper.eq(UserAccount::getUserId,userAccount.getUserId());
        }
        if(userAccount.getMonthArrear() != null){
            queryWrapper.eq(UserAccount::getMonthArrear,userAccount.getMonthArrear());
        }
        if(userAccount.getLateFee() != null){
            queryWrapper.eq(UserAccount::getLateFee,userAccount.getLateFee());
        }
        if(userAccount.getTotalArrear() != null){
            queryWrapper.eq(UserAccount::getTotalArrear,userAccount.getTotalArrear());
        }
        if(userAccount.getMonthCost() != null){
            queryWrapper.eq(UserAccount::getMonthCost,userAccount.getMonthCost());
        }
        if(userAccount.getSettleBalance() != null){
            queryWrapper.eq(UserAccount::getSettleBalance,userAccount.getSettleBalance());
        }
        if(userAccount.getAvailableBalance() != null){
            queryWrapper.eq(UserAccount::getAvailableBalance,userAccount.getAvailableBalance());
        }
        if(userAccount.getUpdatedTime() != null){
            queryWrapper.eq(UserAccount::getUpdatedTime,userAccount.getUpdatedTime());
        }
        return userAccountMapper.selectList(queryWrapper);
    }

    /**
     * 新增当前缴费信息
     * 
     * @param userAccount 当前缴费信息
     * @return 结果
     */
    @Override
    public int insertUserAccount(UserAccount userAccount)
    {
        return userAccountMapper.insert(userAccount);
    }

    /**
     * 修改当前缴费信息
     * 
     * @param userAccount 当前缴费信息
     * @return 结果
     */
    @Override
    public int updateUserAccount(UserAccount userAccount)
    {
        return userAccountMapper.updateById(userAccount);
    }

    /**
     * 批量删除当前缴费信息
     * 
     * @param ids 需要删除的当前缴费信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserAccountByIds(Long[] ids)
    {
        return userAccountMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除当前缴费信息信息
     * 
     * @param id 当前缴费信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserAccountById(Long id)
    {
        return userAccountMapper.deleteById(id);
    }

    @Override
    public UserAccount getUserInfoByUserId(String userId) {

        UsersXb usersXb = usersXbMapper.selectOne(new LambdaQueryWrapper<UsersXb>().eq(UsersXb::getUserId,userId));
        if(usersXb == null){
            return null;
        }
        UserAccount userAccount = userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId,userId));
        if(userAccount == null){
            //新增默认账户信息
            userAccount = new UserAccount();
            userAccount.setUserId(userId);
            userAccount.setMonthArrear(BigDecimal.valueOf(0));
            userAccount.setLateFee(BigDecimal.valueOf(0));
            userAccount.setMonthCost(BigDecimal.valueOf(0));
            userAccount.setSettleBalance(BigDecimal.valueOf(0));
            userAccount.setAvailableBalance(BigDecimal.valueOf(0));
            userAccount.setTotalArrear(BigDecimal.valueOf(0));
            userAccount.setUpdatedTime(DateUtils.getNowDate());
            userAccountMapper.insert(userAccount);
        }
        userAccount.setUserName(usersXb.getUserName());
        userAccount.setUserAddress(usersXb.getUserAddress());
        userAccount.setUserCode(usersXb.getUserCode());
        userAccount.setUserMark(usersXb.getUserMark());
        return userAccount;
    }
}
